package com.spring.util;

import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.joda.time.DateTime;

public class DateUtils {

	public final static String DEFAULT_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";

	public final static String DEFAULT_DATE_FORMAT = "yyyy-MM-dd";

	public final static String SHORT_TIME_FORMAT = "yyyy-MM-dd HH:mm";

	public final static String FULL_SEQ_FORMAT = "yyyyMMddHHmmssSSS";

	public final static String FULL_TIME_FORMAT = "yyyyMMddHHmmss";

	public final static String[] MULTI_FORMAT = { "yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM" };

	public final static DateFormat DEFAULT_TIME_FORMATER = new SimpleDateFormat(DEFAULT_TIME_FORMAT);

	public final static DateFormat DEFAULT_DATE_FORMATER = new SimpleDateFormat(DEFAULT_DATE_FORMAT);

	public final static DateFormat SHORT_TIME_FORMATER = new SimpleDateFormat(SHORT_TIME_FORMAT);

	public final static DateFormat FULL_TIME_FORMATER = new SimpleDateFormat(FULL_TIME_FORMAT);

	public final static String FORMAT_YYYY = "yyyy";

	public final static String FORMAT_YYYYMM = "yyyyMM";

	public final static String FORMAT_YYYYMMDD = "yyyyMMdd";

	public final static DateFormat FORMAT_YYYY_FORMATER = new SimpleDateFormat(FORMAT_YYYY);

	public final static DateFormat FORMAT_YYYYMM_FORMATER = new SimpleDateFormat(FORMAT_YYYYMM);

	public final static DateFormat FORMAT_YYYYMMDD_FORMATER = new SimpleDateFormat(FORMAT_YYYYMMDD);

	public static String formatDate(Date date) {
		if (date == null) {
			return "";
		}
		return DEFAULT_DATE_FORMATER.format(date);
	}

	public static String formatDate(Date date, String format) {
		if (date == null) {
			return null;
		}
		return new SimpleDateFormat(format).format(date);
	}

	public static Integer formatDateToInt(Date date, String format) {
		if (date == null) {
			return null;
		}
		return Integer.valueOf(new SimpleDateFormat(format).format(date));
	}

	public static String formatTime(Date date) {
		if (date == null) {
			return null;
		}
		return DEFAULT_TIME_FORMATER.format(date);
	}

	public static String formatShortTime(Date date) {
		if (date == null) {
			return null;
		}
		return SHORT_TIME_FORMATER.format(date);
	}

	// 得到yyyymmdd格式的时间
	public static String formatYyyymmddTime(Date date) {
		if (date == null) {
			return null;
		}
		return FORMAT_YYYYMMDD_FORMATER.format(date);
	}

	// 得到yyyymmdd格式的时间
	public static String formatYyyymmddNow() {
		return formatYyyymmddTime(DateUtils.currentDate());
	}

	public static String formatFullTime(Date date) {
		if (date == null) {
			return null;
		}
		return FULL_TIME_FORMATER.format(date);
	}

	public static String formatDateNow() {
		return formatDate(DateUtils.currentDate());
	}

	public static String formatTimeNow() {
		return formatTime(DateUtils.currentDate());
	}

	// 返回yyyyMMddHHmmss格式的时间
	public static String foramtFullDateNow() {
		return formatFullTime(DateUtils.currentDate());
	}

	public static Date parseDate(String date, DateFormat df) {
		if (date == null) {
			return null;
		}
		try {
			return df.parse(date);
		} catch (ParseException e) {
			throw new RuntimeException(e);
		}
	}

	public static Date parseTime(String date, DateFormat df) {
		if (date == null) {
			return null;
		}
		try {
			return df.parse(date);
		} catch (ParseException e) {
			throw new RuntimeException(e);
		}
	}

	public static Date parseDate(String date) {
		return parseDate(date, DEFAULT_DATE_FORMATER);
	}

	public static Date parseTime(String date) {
		return parseTime(date, DEFAULT_TIME_FORMATER);
	}

	public static String plusOneDay(String date) {
		DateTime dateTime = new DateTime(parseDate(date).getTime());
		return formatDate(dateTime.plusDays(1).toDate());
	}

	public static String plusOneDay(Date date) {
		DateTime dateTime = new DateTime(date.getTime());
		return formatDate(dateTime.plusDays(1).toDate());
	}

	public static String getHumanDisplayForTimediff(Long diffMillis) {
		if (diffMillis == null) {
			return "";
		}
		long day = diffMillis / (24 * 60 * 60 * 1000);
		long hour = (diffMillis / (60 * 60 * 1000) - day * 24);
		long min = ((diffMillis / (60 * 1000)) - day * 24 * 60 - hour * 60);
		long se = (diffMillis / 1000 - day * 24 * 60 * 60 - hour * 60 * 60 - min * 60);
		StringBuilder sb = new StringBuilder();
		if (day > 0) {
			sb.append(day + "D");
		}
		DecimalFormat df = new DecimalFormat("00");
		sb.append(df.format(hour) + ":");
		sb.append(df.format(min) + ":");
		sb.append(df.format(se));
		return sb.toString();
	}

	/**
	 * 把类似2014-01-01 ~ 2014-01-30格式的单一字符串转换为两个元素数组
	 */
	public static Date[] parseBetweenDates(String date) {
		if (StringUtil.isBlank(date)) {
			return null;
		}
		date = date.replace("～", "~");
		Date[] dates = new Date[2];
		String[] values = date.split("~");
		dates[0] = parseMultiFormatDate(values[0].trim());
		dates[1] = parseMultiFormatDate(values[1].trim());
		return dates;
	}

	public static Date parseMultiFormatDate(String date) {
		try {
			return org.apache.commons.lang3.time.DateUtils.parseDate(date, MULTI_FORMAT);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 将日期格式的字符串转换为长整型
	 * yyyy-MM-dd HH:mm:ss
	 * @param date
	 * @param format
	 * @return
	 */
	public static Long convertToLong(String date) {
		try {
			if (!StringUtils.isEmpty(date)) {
				return DEFAULT_TIME_FORMATER.parse(date).getTime();
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return 0l;
	}

	/**
	 * 将长整型数字转换为日期格式的字符串
	 * yyyy-MM-dd HH:mm:ss
	 * @param time
	 * @param format
	 * @return
	 */
	public static String convertToString(long time) {
		if (time > 0l) {
			Date date = new Date(time);

			return DEFAULT_TIME_FORMATER.format(date);
		}
		return "";
	}

	/**
	 * @Title:getDiffDay
	 * @Description:获取日期相差天数
	 * @param:@param beginDate
	 *                   字符串类型开始日期
	 * @param:@param endDate
	 *                   字符串类型结束日期
	 * @param:@return
	 * @return:Long 日期相差天数
	 * @author:谢
	 * @thorws:
	 */
	public static Long getDiffDay(String beginDate, String endDate) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		Long checkday = 0l;
		// 开始结束相差天数
		try {
			checkday = (formatter.parse(endDate).getTime() - formatter.parse(beginDate).getTime())
					/ (1000 * 24 * 60 * 60);
		} catch (ParseException e) {

			e.printStackTrace();
			checkday = null;
		}
		return checkday;
	}

	/**
	 * @Title:getDiffDay
	 * @Description:获取日期相差天数
	 * @param:@param beginDate
	 *                   Date类型开始日期
	 * @param:@param endDate
	 *                   Date类型结束日期
	 * @param:@return
	 * @return:Long 相差天数
	 * @author: 谢
	 * @thorws:
	 */
	public static Long getDiffDay(Date beginDate, Date endDate) {
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String strBeginDate = format.format(beginDate);

		String strEndDate = format.format(endDate);
		return getDiffDay(strBeginDate, strEndDate);
	}

	/**
	 * N天之后
	 * 
	 * @param n
	 * @param date
	 * @return
	 */
	public static Date nDaysAfter(Integer n, Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH) + n);
		return cal.getTime();
	}

	/**
	 * N天之前
	 * 
	 * @param n
	 * @param date
	 * @return
	 */
	public static Date nDaysAgo(Integer n, Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.DAY_OF_MONTH, cal.get(Calendar.DAY_OF_MONTH) - n);
		return cal.getTime();
	}

	/**
	 * N月之前
	 * 
	 * @param n
	 * @param date
	 * @return
	 */

	public static Date nMonthAgo(Integer n, Date date) {
		GregorianCalendar gcLast = (GregorianCalendar) Calendar.getInstance();
		gcLast.setTime(date);
		gcLast.add(Calendar.MONTH, -n);
		return gcLast.getTime();
	}

	public static String getAge(Date birthDay) {
		String age = "";
		try {
			Date date = new Date();
			long day = (date.getTime() - birthDay.getTime()) / (24 * 60 * 60 * 1000) + 1;
			age = new java.text.DecimalFormat("#").format(day / 365f);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return age;
	}

	public static Date currentDate() {
		return new Date();
	}

	public static String currentDate(String format) {
		return formatDate(new Date(), format);
	}

	public static void main(String[] args) {
		
	}

	// 获取当前日期的字符串表达式--不就是getTime么
	public static Date getDate() {
		Calendar calendar = Calendar.getInstance();
		Date date = calendar.getTime();
		return date;
	}
	
	  /**
     * 切割时间段
     *
     * @param start HH:mm
     * @param end   HH:mm
     * @return
     */
    public static List<String> spiltTime(String start, String end)  {
        List<String> listTime = new ArrayList<>();
        listTime.add(start);
        try{
	        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
	        Date dBegin = sdf.parse(start);
	        Date dEnd = sdf.parse(end);
	        Calendar calBegin = Calendar.getInstance();
	        calBegin.setTime(dBegin);
	        Calendar calEnd = Calendar.getInstance();
	        calEnd.setTime(dEnd);
	        while (calEnd.after(calBegin)) {
	        	  calBegin.add(Calendar.HOUR, 1);
	            if (calEnd.after(calBegin))
	            	listTime.add(sdf.format(calBegin.getTime()));
	            else
	            	listTime.add(sdf.format(calEnd.getTime()));
	        }
        }catch(Exception ex)
        {
        	ex.printStackTrace();
        }
        return listTime;
    }
    
    
    /**
     * 根据日期获得星期 
     * @param date
     * @param type  获取数字 type值为code，获取周几type为name
     * @return
     */
	public static String getWeekOfDate(Date date,String type) { 
	  String[] weekDaysName = {"周日","周一", "周二", "周三", "周四", "周五", "周六", }; 
	  String[] weekDaysCode = {"7","1", "2", "3", "4", "5", "6"}; 
	  Calendar calendar = Calendar.getInstance(); 
	  calendar.setTime(date); 
	  int intWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1; 
	  if(type.equals("code")){
	     return weekDaysCode[intWeek];
	  }
	  if(type.equals("name")){
	     return weekDaysName[intWeek];
	  }
	  return null;
	} 

	public static boolean isThisTime(Date date, String pattern) {
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		String param = sdf.format(date);// 参数时间
		String now = sdf.format(new Date());// 当前时间
		if (param.equals(now)) {
			return true;
		}
		return false;
	}


}
